  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="generator" content="pandoc" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>GTK 4 tutorial</title>
    <style>
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
      div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
      ul.task-list{list-style: none;}
      pre{overflow: visible;}
      pre > code.sourceCode { white-space: pre; position: relative; }
      pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
      pre > code.sourceCode > span:empty { height: 1.2em; }
      code.sourceCode > span { color: inherit; text-decoration: inherit; }
      div.sourceCode { margin: 1em 0; }
      pre.sourceCode { margin: 0; }
      @media screen {
      div.sourceCode { overflow: auto; }
      }
      @media print {
      pre > code.sourceCode { white-space: pre-wrap; }
      pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
      }
      pre.numberSource code
        { counter-reset: source-line 0; }
      pre.numberSource code > span
        { position: relative; left: -4em; counter-increment: source-line; }
      pre.numberSource code > span > a:first-child::after
        { content: counter(source-line);
          position: relative; left: -1em; text-align: right; vertical-align: baseline;
          border: none; display: inline-block;
          -webkit-touch-callout: none; -webkit-user-select: none;
          -khtml-user-select: none; -moz-user-select: none;
          -ms-user-select: none; user-select: none;
          padding: 0 4px; width: 4em;
          color: #aaaaaa;
        }
      pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
      div.sourceCode
        {   }
      @media screen {
      pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
      }
      code span.al { color: #ff0000; font-weight: bold; } /* Alert */
      code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
      code span.at { color: #7d9029; } /* Attribute */
      code span.bn { color: #40a070; } /* BaseN */
      code span.bu { } /* BuiltIn */
      code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
      code span.ch { color: #4070a0; } /* Char */
      code span.cn { color: #880000; } /* Constant */
      code span.co { color: #60a0b0; font-style: italic; } /* Comment */
      code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
      code span.do { color: #ba2121; font-style: italic; } /* Documentation */
      code span.dt { color: #902000; } /* DataType */
      code span.dv { color: #40a070; } /* DecVal */
      code span.er { color: #ff0000; font-weight: bold; } /* Error */
      code span.ex { } /* Extension */
      code span.fl { color: #40a070; } /* Float */
      code span.fu { color: #06287e; } /* Function */
      code span.im { } /* Import */
      code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
      code span.kw { color: #007020; font-weight: bold; } /* Keyword */
      code span.op { color: #666666; } /* Operator */
      code span.ot { color: #007020; } /* Other */
      code span.pp { color: #bc7a00; } /* Preprocessor */
      code span.sc { color: #4070a0; } /* SpecialChar */
      code span.ss { color: #bb6688; } /* SpecialString */
      code span.st { color: #4070a0; } /* String */
      code span.va { color: #19177c; } /* Variable */
      code span.vs { color: #4070a0; } /* VerbatimString */
      code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
      div.sourceCode { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
      pre:not(.sourceCode) { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
      table {margin-left: auto; margin-right: auto; border-collapse: collapse; border: 1px solid;}
      th {padding: 2px 6px; border: 1px solid; background-color: ghostwhite;}
      td {padding: 2px 6px; border: 1px solid;}
      img {display: block; margin-left: auto; margin-right: auto;}
      figcaption {text-align: center;}
    </style>
  </head>
  <body style="padding-top: 70px;">
    <div class="container">
    <nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
      <div class="container-fluid">
        <span class="navbar-brand">Gtk4 tutorial</span>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav me-auto mb-2 mb-lg-0">
            <li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>

            <li class="nav-item">
<a class="nav-link" href="sec23.html">Prev: section23</a>
</li>

            <li class="nav-item">
<a class="nav-link" href="sec25.html">Next: section25</a>
</li>

          </ul>
        </div>
      </div>
    </nav>
<h1 id="combine-gtkdrawingarea-and-tfetextview">Combine GtkDrawingArea
and TfeTextView</h1>
<p>Now, we will make a new application which has GtkDrawingArea and
TfeTextView in it. Its name is “color”. If you write a name of a color
in TfeTextView and click on the <code>run</code> button, then the color
of GtkDrawingArea changes to the color given by you.</p>
<figure>
<img src="image/color.png" alt="color" />
<figcaption aria-hidden="true">color</figcaption>
</figure>
<p>The following colors are available. (without new line charactor)</p>
<ul>
<li>white</li>
<li>black</li>
<li>red</li>
<li>green</li>
<li>blue</li>
</ul>
<p>In addition the following two options are also available.</p>
<ul>
<li>light: Make the color of the drawing area lighter.</li>
<li>dark: Make the color of the drawing area darker.</li>
</ul>
<p>This application can only do very simple things. However, it tells us
that if we add powerful parser to it, we will be able to make it more
efficient. I want to show it to you in the later section by making a
turtle graphics language like Logo program language.</p>
<p>In this section, we focus on how to bind the two objects.</p>
<h2 id="color.ui-and-color.gresource.xml">Color.ui and
color.gresource.xml</h2>
<p>First, We need to make the ui file of the widgets. Title bar, four
buttons in the tool bar, textview and drawing area. The ui file is as
follows.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/color.ui @@@</p>
<ul>
<li>10-53: The horizontal box <code>boxh1</code> makes a tool bar which
has four buttons, <code>Run</code>, <code>Open</code>, <code>Save</code>
and <code>Close</code>. This is similar to the <code>tfe</code> text
editor in <a href="sec9.html">Section 9</a>. There are two differences.
<code>Run</code> button replaces <code>New</code> button. A signal
element is added to each button object. It has “name” attribute which is
a signal name and “handler” attribute which is the name of its signal
handler. Options “-WI, –export-dynamic” CFLAG is necessary when you
compile the application. You can achieve this by adding “export_dynamic:
true” argument to the executable function in <code>meson.build</code>.
And be careful that the handler must be defined without ‘static’
class.</li>
<li>54-76: The horizontal box <code>boxh2</code> includes
GtkScrolledWindow and GtkDrawingArea. GtkBox has “homogeneous property”
with TRUE value, so the two children have the same width in the box.
TfeTextView is a child of GtkScrolledWindow.</li>
</ul>
<p>The xml file for the resource compiler is almost same as before. Just
substitute “color” for “tfe”.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/color.gresource.xml @@@</p>
<h2 id="drawing-function-and-surface">Drawing function and surface</h2>
<p>The main point of this program is a drawing function.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/colorapplication.c draw_func @@@</p>
<p>The <code>surface</code> variable in line 3 is a static variable.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="dt">static</span> cairo_surface_t <span class="op">*</span>surface <span class="op">=</span> NULL<span class="op">;</span></span></code></pre></div>
<p>The drawing function just copies the <code>surface</code> to its own
surface with the <code>cairo_paint</code> function. The surface (pointed
by the static variable <code>surface</code>) is built by the
<code>run</code> function.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/colorapplication.c run @@@</p>
<ul>
<li>9-10: Gets the string in the GtkTextBuffer and inserts it to
<code>contents</code>.</li>
<li>11: If the variable <code>surface</code> points a surface instance,
it is painted as follows.</li>
<li>12- 30: The source is set based on the string <code>contents</code>
and copied to the surface with <code>cairo_paint</code>.</li>
<li>24,26: Alpha channel is used in “light” and “dark” procedure.</li>
</ul>
<p>The drawing area just reflects the <code>surface</code>. But one
problem is resizing. If a user resizes the main window, the drawing area
is also resized. It makes size difference between the surface and the
drawing area. So, the surface needs to be resized to fit the drawing
area.</p>
<p>It is accomplished by connecting the “resize” signal on the drawing
area to a handler.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>g_signal_connect <span class="op">(</span>GTK_DRAWING_AREA <span class="op">(</span>da<span class="op">),</span> <span class="st">&quot;resize&quot;</span><span class="op">,</span> G_CALLBACK <span class="op">(</span>resize_cb<span class="op">),</span> NULL<span class="op">);</span></span></code></pre></div>
<p>The handler is as follows.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/colorapplication.c resize_cb @@@</p>
<p>If the variable <code>surface</code> sets a surface instance, it is
destroyed. A new surface is created and its size fits the drawing area.
The surface is assigned to the variable <code>surface</code>. The
function <code>run</code> is called and the surface is colored.</p>
<p>The signal is emitted when:</p>
<ul>
<li>The drawing area is realized (it appears on the display).</li>
<li>It is changed (resized) while realized</li>
</ul>
<p>So, the first surface is created when it is realized.</p>
<h2 id="colorapplication.c">Colorapplication.c</h2>
<p>This is the main file.</p>
<ul>
<li>Builds widgets by GtkBuilder.</li>
<li>Sets a drawing function for GtkDrawingArea. And connects a handler
to the “resize” signal on the GtkDrawingArea instance.</li>
<li>Implements each call back function. Particularly, <code>Run</code>
signal handler is the point in this program.</li>
</ul>
<p>The following is <code>colorapplication.c</code>.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/colorapplication.c @@@</p>
<ul>
<li>4-8: Win, tv, da and surface are defined as static variables.</li>
<li>10-42: Run function.</li>
<li>44-63: Handlers for button signals.</li>
<li>65-71: Resize handler.</li>
<li>73-79: Drawing function.</li>
<li>81-84: Application activate handler. It just shows the main
window.</li>
<li>86-105: Application startup handler.</li>
<li>92- 97: It builds widgets according to the ui resource. The static
variables win, tv and da are assigned instances.</li>
<li>98: Connects “resize” signal and a handler.</li>
<li>99: Drawing function is set.</li>
<li>101-104: CSS for textview padding is set.</li>
<li>107-111: Application shutdown handler. If there exists a surface
instance, it will be destroyed.</li>
<li>116-129: A function <code>main</code>. It creates a new application
instance. And connects three signals startup, shutdown and activate to
their handlers. It runs the application. It releases the reference to
the application and returns with <code>stat</code> value.</li>
</ul>
<h2 id="meson.build">Meson.build</h2>
<p>This file is almost same as before. An argument “export_dynamic:
true” is added to executable function.</p>
<p>@@<span class="citation" data-cites="include">@include</span>
color/meson.build @@@</p>
<h2 id="build-and-try">Build and try</h2>
<p>Type the following to compile the program.</p>
<pre><code>$ meson _build
$ ninja -C _build</code></pre>
<p>The application is made in <code>_build</code> directory. Type the
following to execute it.</p>
<pre><code>$ _build/color</code></pre>
<p>Type “red”, “green”, “blue”, “white”, black”, “light” or “dark” in
the TfeTextView. No new line charactor is needed. Then, click on the
<code>Run</code> button. Make sure the color of GtkDrawingArea
changes.</p>
<p>In this program TfeTextView is used to change the color. You can use
buttons or menus instead of textview. Probably it is more appropriate.
Using textview is unnatural. It is a good practice to make such
application by yourself.</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
  </body>
  </html>
